﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;

namespace Site_CamaraTibau
{
    public partial class VerNoticias : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!Page.IsPostBack)
                {
                    VereadorBO.Listar(ddlVereador);

                    ddlVereador.Items[0].Text = "Todos";

                    atualizaListagemNoticias();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region Contexto
        ModeloContainer bco = new ModeloContainer();
        #endregion

        protected void lbkVerBusca_Click(object sender, EventArgs e)
        {
            if (div_busca.Visible == true)
                div_busca.Visible = false;
            else
                div_busca.Visible = true;
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            atualizaListagemNoticias();
        }
        
        public void atualizaListagemNoticias()
        {
            try
            {
                bco = new ModeloContainer();

                var objs = (from a in bco.Noticias select a).ToList();


                if (ddlVereador.SelectedIndex > 0)
                {
                    int id = int.Parse(ddlVereador.SelectedValue.ToString());

                    objs = (from a in objs
                            join b in bco.NoticiaVereadors on a.id equals b.noticiaID
                            where b.vereadorID == id
                            select a).ToList();
                }

                if (ddlDia.SelectedIndex > 0)
                {
                    int dia = int.Parse(ddlDia.SelectedValue.ToString());

                    objs = objs.Where(x => x.dtPublicacao.Value.Day == dia).ToList();
                }

                if (ddlMes.SelectedIndex > 0)
                {
                    int mes = int.Parse(ddlMes.SelectedValue.ToString());

                    objs = objs.Where(x => x.dtPublicacao.Value.Month == mes).ToList();
                }

                if (ddlAno.SelectedIndex > 0)
                {
                    int ano = int.Parse(ddlMes.SelectedValue.ToString());

                    objs = objs.Where(x => x.dtPublicacao.Value.Year == ano).ToList();
                }

                if (!string.IsNullOrEmpty(txtPesquisarTitulo.Text))
                {
                    objs = objs.Where(x => x.titulo.ToLower().Contains(txtPesquisarTitulo.Text.ToLower().Trim())).ToList();
                }


                if (!string.IsNullOrEmpty(txtPesquisarConteudo.Text))
                {
                    objs = objs.Where(x => x.texto.ToLower().Contains(txtPesquisarConteudo.Text.ToLower().Trim())).ToList();
                }


                var objs2 = (from a in objs
                             orderby a.dtPublicacao descending
                             select new
                             {
                                 ID = a.id,
                                 Descricao = a.dtPublicacao.Value.Day + "/" + a.dtPublicacao.Value.Month + " - " + a.titulo,
                                 LINK = "~/VerNoticia.aspx?ID=" + a.id
                             }).ToList();

                rptNoticias.DataSource = objs2;
                rptNoticias.DataBind();

            }

            catch (Exception ex)
            {

            }

        }
    }
}